﻿@page "/UserAccounts/edit"

@using Microsoft.EntityFrameworkCore
@using BlazorAppAuth.Models
@inject IDbContextFactory<BlazorAppAuth.Data.BlazorAppAuthContext> DbFactory
@inject NavigationManager NavigationManager

<PageTitle>Edit</PageTitle>

<h1>Edit</h1>

<h4>UserAccount</h4>
<hr />
@if (UserAccount is null)
{
    <p><em>Loading...</em></p>
}
else
{
    <div class="row">
        <div class="col-md-4">
            <EditForm method="post" Model="UserAccount" OnValidSubmit="UpdateUserAccount" FormName="edit" Enhance>
                <DataAnnotationsValidator />
                <ValidationSummary />
                <input type="hidden" name="UserAccount.Id" value="@UserAccount.Id" />
                <div class="mb-3">
                    <label for="name" class="form-label">Name:</label>
                    <InputText id="name" @bind-Value="UserAccount.Name" class="form-control" />
                    <ValidationMessage For="() => UserAccount.Name" class="text-danger" />
                </div>
                <div class="mb-3">
                    <label for="password" class="form-label">Password:</label>
                    <InputText id="password" @bind-Value="UserAccount.Password" class="form-control" />
                    <ValidationMessage For="() => UserAccount.Password" class="text-danger" />
                </div>
                <div class="mb-3">
                    <label for="role" class="form-label">Role:</label>
                    <InputText id="role" @bind-Value="UserAccount.Role" class="form-control" />
                    <ValidationMessage For="() => UserAccount.Role" class="text-danger" />
                </div>
                <button type="submit" class="btn btn-primary">Save</button>
            </EditForm>
        </div>
    </div>
}

<div>
    <a href="/useraccounts">Back to List</a>
</div>

@code {
    [SupplyParameterFromQuery]
    public int Id { get; set; }

    [SupplyParameterFromForm]
    public UserAccount? UserAccount { get; set; }

    protected override async Task OnInitializedAsync()
    {
        using var context = DbFactory.CreateDbContext();
        UserAccount ??= await context.UserAccount.FirstOrDefaultAsync(m => m.Id == Id);

        if (UserAccount is null)
        {
            NavigationManager.NavigateTo("notfound");
        }
    }

    // To protect from overposting attacks, enable the specific properties you want to bind to.
    // For more details, see https://aka.ms/RazorPagesCRUD.
    public async Task UpdateUserAccount()
    {
        using var context = DbFactory.CreateDbContext();
        context.Attach(UserAccount!).State = EntityState.Modified;

        try
        {
            await context.SaveChangesAsync();
        }
        catch (DbUpdateConcurrencyException)
        {
            if (!UserAccountExists(UserAccount!.Id))
            {
                NavigationManager.NavigateTo("notfound");
            }
            else
            {
                throw;
            }
        }

        NavigationManager.NavigateTo("/useraccounts");
    }

    bool UserAccountExists(int id)
    {
        using var context = DbFactory.CreateDbContext();
        return context.UserAccount.Any(e => e.Id == id);
    }
}
